package com.sims.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.sims.eo.SimsStudent;
import com.sims.service.IStudentService;
import com.sims.service.impl.StudentServiceImp;

public class StudentPageAction extends ActionSupport {
	String studentId;
	IStudentService studentService = new StudentServiceImp();
	SimsStudent student = null;

	// -----------分页获取数据---------------
	private int pageNo = 1;
	private int pageSize = 4;
	private int pageCount;
	private long rowCount;

	public String getStudentId() {
		return studentId;
	}

	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}

	public SimsStudent getStudent() {
		return student;
	}

	public void setStudent(SimsStudent student) {
		this.student = student;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public long getRowCount() {
		return rowCount;
	}

	public void setRowCount(long rowCount) {
		this.rowCount = rowCount;
	}

	// 获取所有账号
	public String listStudent() throws Exception {
		System.out.println("-----listStudentPage");
		rowCount = studentService.getRowCount();
		// 获取总页数
		if (rowCount % pageSize == 0) {
			pageCount = (int) (rowCount / pageSize);
		} else {
			pageCount = (int) (rowCount / pageSize) + 1;
		}
		if (pageNo < 1) {
			pageNo = 1;
		} else if (pageNo > pageCount) {
			pageNo = pageCount;
		}
		// 分页获取记录
		List<SimsStudent> list = studentService.getAllSimsStudent(pageNo,
				pageSize);
		ActionContext ctx = ActionContext.getContext();
		ctx.put("StudentList", list);
		return "list";
	}

	// 删除账号
	public String deleteStudent() throws Exception {
		System.out.println("-----deleteStudent id=" + studentId);
		Long id = Long.parseLong(studentId);
		boolean bFlag = studentService.deletSimsStudentById(id);
		if (bFlag) {
			this.addActionMessage("删除数据成功！");
		} else {
			this.addActionMessage("删除数据失败！");
		}
		return listStudent();
	}

	// 修改账号
	public String editStudent() throws Exception {
		System.out.println("-----editStudent id=" + studentId);
		Long id = Long.parseLong(studentId);
		student = studentService.getSimsStudentById(id);
		if (student == null) {
			// 未获取到用户对象给提示
			this.addActionError("id=" + id + ",未获取到用户对象！");
		}
		return "edit";
	}

	// 编辑账号
	@InputConfig(resultName = "edit_input")
	public String updateStudent() throws Exception {
		System.out.println("-----updateStudent");
		boolean bFlag = studentService.updateSimsStudent(student);
		if (bFlag) {
			// 修改成功
			return "go_list";
		} else {
			this.addActionError("学号:" + student.getStudentNumber()
					+ "已存在,请重新填写！");
			return "edit";
		}
	}

	// 编辑账号Action校验方法
	public void validateUpdateStudent() throws Exception {
		System.out.println("----validateUpdateStudent");
		if (student == null) {
			// 未获取到用户对象给提示
			this.addActionError("未获取到用户对象！");
		} else {
			if (student.getStudentName() == null
					|| student.getStudentName().trim().length() <= 0) {
				this.addFieldError("studentNamepageInfo", "请输入名字 ！");
			}
			if (student.getStudentNumber() == null
					|| student.getStudentNumber().trim().length() <= 0) {
				this.addFieldError("studentNumberpageInfo", "请输入学号！");
			}
			if (student.getSex() == null
					|| student.getSex().trim().length() <= 0) {
				this.addFieldError("studentSexpageInfo", "请填写性别！");
			}
			if (student.getDepartment() == null
					|| student.getDepartment().trim().length() <= 0) {
				this.addFieldError("studentDepartmentpageInfo", "请填写系部 ！");
			}
			if (student.getMajor() == null
					|| student.getMajor().trim().length() <= 0) {
				this.addFieldError("studentMajorpageInfo", "请填写专业！");
			}
		}
	}

	@InputConfig(resultName = "create_input")
	public String createStudent() throws Exception {
		System.out.println("----createStudent---");
		Long id = studentService.createstudent(student);
		if (id > 0) {
			// 新建成功
			this.addActionError("学号:" + student.getStudentNumber() + ",新建成功！");
			return listStudent();
		} else {
			// 新建失败
			this.addActionError("学号:" + student.getStudentNumber()
					+ "已存在,请重新填写！");
			return "create";
		}
	}

	// 注册Action校验方法
	public void validateCreateStudent() throws Exception {
		System.out.println("----validateCreateStudent---");
		if (student.getStudentName() == null
				|| student.getStudentName().trim().length() <= 0) {
			this.addFieldError("studentNameInfo", "请填写姓名 ！");
		}
		if (student.getStudentNumber() == null
				|| student.getStudentNumber().trim().length() <= 0) {
			this.addFieldError("studentNumberInfo", "请填写学号 ！");
		}
		if (student.getSex() == null || student.getSex().trim().length() <= 0
				|| student.getSex().trim().length() > 1) {
			this.addFieldError("studentSexInfo", "请填写性别 ！");
		}
		if (student.getDepartment() == null
				|| student.getDepartment().trim().length() <= 0) {
			this.addFieldError("studentDepartmentInfo", "请填写系部 ！");
		}
		if (student.getMajor() == null
				|| student.getMajor().trim().length() <= 0) {
			this.addFieldError("studentMajorInfo", "请填写专业 ！");
		}
	}
}
